package hsb.ruoyi.common.core.domain.model;


import com.fasterxml.jackson.annotation.JsonIgnore;
import hsb.ruoyi.common.core.domain.entity.AppUser;
import hsb.ruoyi.common.core.domain.entity.SysUser;
import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 登录用户身份权限
 *
 * @author ruoyi
 */
@RegisterForReflection
@Data
public class LoginUser implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 用户体系前缀，如果有多张用户表，没使用全局统一id时，可以设置这个字段来做区分
     * <p>
     * 用户在不同数据库表时，存在id相同情况，使用前缀做用户id区分
     * 目前； 后台用户 sys   app用户 app
     */
    private String typePrefix;

    private String userName;
    /**
     * 部门ID
     */
    private Long deptId;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private long loginTime;

    /**
     * 过期时间
     */
    private long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 用户信息
     */
    private SysUser user;

    private AppUser appUser;


    private List<String> roles;


    public String getTypePrefix() {
//        if (user != null) {
//            return "sys_";
//        } else if (appUser != null) {
//            return "app_";
//        }
        return typePrefix != null ? typePrefix : "";
    }

    public LoginUser() {
    }

    public LoginUser(SysUser user) {
        this.user = user;
        this.userName = user.getUserName();
    }

    public LoginUser(Long userId, Long deptId, SysUser user) {
        this.userId = userId;
        this.deptId = deptId;
        this.user = user;
        this.userName = user.getUserName();
    }

    public LoginUser(Long userId, Long deptId, SysUser user,List<String> roles) {
        this.userId = userId;
        this.deptId = deptId;
        this.user = user;
        this.userName = user.getUserName();
        this.roles = roles;
        this.typePrefix = "sys_";
    }

    public LoginUser(Long userId, Long deptId, AppUser user) {
        this.userId = userId;
        this.deptId = deptId;
        this.appUser = user;
        this.userName = user.getNickName();
        this.typePrefix = "app_";
    }


    @JsonIgnore
    public String getPassword() {
        if (user != null) {
            return user.getPassword();
        } else {
            return appUser.getPassword();
        }
    }


    public String getUserName() {
        return userName;
    }


    public AppUser getAppUser() {
        return appUser;
    }

    public void setAppUser(AppUser appUser) {
        this.appUser = appUser;
    }
}
